//
//  AddDrugCommentViewController.m
//  安好
//
//  Created by lianchuang on 14/12/10.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "AddDrugCommentViewController.h"

@interface AddDrugCommentViewController ()<UITextViewDelegate>

@end

@implementation AddDrugCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBackDown)];
    
    backBtnItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnItem;
    self.commentText.delegate=self;
    self.navigationItem.title=@"点评";
    CGRect rect = self.commentText.frame;
    rect.size.width = kScreenWidth - 30;
    self.commentText.frame = rect;
    
    rect = self.submit.frame;
    rect.size.width = kScreenWidth - 30;
    self.submit.frame = rect;
    // Do any additional setup after loading the view from its nib.
}
- (void)leftBackDown
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark UITextViewDelegate textView的代理方法
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    self.labeltext.hidden=YES;//隐藏文字
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length>0) {
        self.labeltext.hidden=YES;
        
    }else{
        self.labeltext.hidden=NO;//隐藏文字
    }
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    if (textView.text.length==0){//textview长度为0
        if ([text isEqualToString:@""]) {//判断是否为删除键
            self.labeltext.hidden=YES;//隐藏文字
        }else{
            self.labeltext.hidden=YES;
        }
    }else{//textview长度不为0
        if (textView.text.length==1){//textview长度为1时候
            if ([text isEqualToString:@""]) {//判断是否为删除键
                self.labeltext.hidden=NO;
            }else{//不是删除
                self.labeltext.hidden=YES;
            }
        }else{//长度不为1时候
            self.labeltext.hidden=YES;
        }
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)dosubmit:(UIButton *)sender {
    if (self.commentText.text.length<=0)
    {
         [self showCustomHudSingleLine:@"评价不完整，请您重新评价!"];
    }else
    {
        [self showActionView];
        NSString * postURL = [NSString stringWithFormat:@"%@wiki/addDrugComment",kBaseURL];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSDictionary *parameters=@{
                                   @"platform":kPlatform,
                                   @"version":kVersion,
                                   @"drugid":self.drugid,
                                   @"comment":self.commentText.text,
                                   };
        [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [self hiddenActionView];
            NSString *status=[NSString stringWithFormat:@"%@",[responseObject valueForKey:@"status"]];
            if ([status isEqualToString:@"1"]) {
                [self.navigationController popViewControllerAnimated:YES];
            }else
            {
                [self showCustomHudSingleLine:[responseObject valueForKey:@"msg"]];
            }
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [self hiddenActionView];
        }];
    }
}
@end
